//引入express
const { json } = require('body-parser');
var express = require('express');
//引入路由
var apps = express.Router();
var UUID = require('uuid');
let db = require('../utils/db');
apps.get('/ceshiget111111', function (req, res) {
  let sql = 'select * from user';
  db.query(sql, function (err, data) {
    res.json({ val: data });
  })
})
apps.post('/ceshipost', function (req, res) {
  res.json({ val: 'huangying' });
})

//pc 用户管理
//查询所有用户
apps.get('/getAllUserM', function (req, res) {
  console.log(req.query);
  let sql2 = 'select * from user limit  ' + (req.query.page - 1) * req.query.pageSize + ',' + req.query.pageSize * req.query.page + ''
  let sql1 = 'select count(id) count from user';
  let storesql = 'select name from store';
  let ordersql = 'SELECT COUNT(*) count FROM orderr where user';
  db.query(sql2, function (err, data) {
    // console.log(data);
    if (data) {
      db.query(sql1, function (err, data1) {
        res.json({
          data: {
            data, count: data1[0].count,
            msg: '查询成功',
            code: 200

          }
        })
      })
    } else {
      res.json({
        msg: '查询失败'
      })
    }
  })
})
//pc端 条件查询
apps.get('/getWhere', function (req, res) {
  let user_name = 'SELECT * FROM `shopping-mall`.user where tel like "%'+req.query.inp+'%" or user_name like "%'+req.query.inp+'%" ';
  db.query(user_name, function (err, data) {
    if (data) {
      res.json({
        code: 200,
        msq: "查询成功",
        data
      });
    } else {
      res.json({
        code: 500,
        msq: "查询失败",
        err: err,
      });
    }
  })
})
//c端 
// 订单查询
apps.get('/c_order', function (req, res) {

  let ordersql = 'SELECT *,c.name store_name,a.id order_id FROM orderr a LEFT JOIN user b ON  a.user_id=b.id LEFT JOIN store c ON a.store_id=c.id LEFT JOIN goods d ON d.id=a.goods_id where a.deleted=0 '
  let goodscql = 'select * from order_goods_center'
  db.query(ordersql, (err, data) => {
    // let data=data;
    if (data) {
      db.query(goodscql, (err, data1) => {
        if (data1) {
          for (let v of data) {
            switch (v.state) {
              case 1: v.state = '待付款'; break
              case 2: v.state = '待发货'; break
              case 3: v.state = '待收货'; break
              case 4: v.state = '已完成'; break
              case 5: v.state = '已失效'; break
            }
            console.log(v.state);
          }
          res.json({
            code: 200,
            msg: '订单查询成功',
            data: {
              data: data,
              data1: data1
            }

          })
        }
      })
    } else {
      res.json({
        code: 500,
        msg: '订单查询失败'
      })
    }
  })
})
//b端 添加
apps.post('/b_message',function(req,res){
  console.log(req.body);
  let sql='UPDATE `shopping-mall`.`store` SET `name` = ?, `logo` = ?, `wx_number` = ?  WHERE `id` = ?'
  db.query(sql,[req.body.name,req.body.logo,req.body.wx_number,req.body.id],(err,data)=>{
    if(data){
      res.json({
        code:200,
        msg:'更新成功',
        data:data
      })
    }else{
      res.json({
        code:500,
        msg:'更新失败'
      })
    }
  })
})
module.exports = apps;